﻿<!DOCTYPE HTML>
<html lang="zh">
<head>
<title>进程函数列表 | AutoHotkey v2</title>
<meta name="description" content="This page lists functions for performing the following operations on a process: check if it exists; change its priority; close it; wait for it to exist; wait for it to close." />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link href="../static/theme.css" rel="stylesheet" type="text/css" />
<script src="../static/content.js" type="text/javascript"></script>
<script type="text/javascript">$(function(){0<=window.navigator.userAgent.toLowerCase().indexOf("ucbrowser")&&CaoNiMaDeUc()})</script>
</head>
<body>

<h1>进程函数</h1>

<p>Functions for performing the following operations on a process: check if it exists; change its priority; close it; wait for it to exist; wait for it to close. Click on a function name for details.</p>
<table class="info">
    <tr>
        <th>Function</th>
        <th>Description</th>
    </tr>
    <tr>
        <td><a href="ProcessClose.htm">ProcessClose</a></td>
        <td>强制关闭第一个匹配的进程.</td>
    </tr>
    <tr>
        <td><a href="ProcessExist.htm">ProcessExist</a></td>
        <td>检查指定的进程是否存在.</td>
    </tr>
    <tr>
        <td><a href="ProcessSetPriority.htm">ProcessSetPriority</a></td>
        <td>更改第一个匹配进程的优先级.</td>
    </tr>
    <tr>
        <td><a href="ProcessWait.htm">ProcessWait</a></td>
        <td>Waits for the specified process to exist.</td>
    </tr>
    <tr>
        <td><a href="ProcessWaitClose.htm">ProcessWaitClose</a></td>
        <td>Waits for all matching processes to close.</td>
    </tr>
</table>

<h2 id="Remarks">备注</h2>
<p><strong>Process list</strong>: Although there is no <em>ProcessList</em> function, <a href="#ExList">示例 #1</a> and <a href="#ExListCOM">示例 #2</a> demonstrate how to retrieve a list of processes via <a href="DllCall.htm">DllCall</a> or COM.</p>

<h2 id="Related">相关</h2>
<p><a href="Run.htm">Run</a>, <a href="WinClose.htm">WinClose</a>, <a href="WinKill.htm">WinKill</a>, <a href="WinWait.htm">WinWait</a>, <a href="WinWaitClose.htm">WinWaitClose</a>, <a href="WinExist.htm">WinExist</a>, <a href="Win.htm">Win functions</a></p>

<h2 id="Examples">示例</h2>

<div class="ex" id="ExList">
<p><a href="#ExList">#1</a>: Retrieves a list of running processes via <a href="DllCall.htm">DllCall</a> then shows them in a message box:</p>
<pre>d := "  |  "  <em>; string separator</em>
s := 4096  <em>; size of buffers and arrays (4 KB)</em>

ScriptPID := ProcessExist()  <em>; The PID of this running script.</em>
<em>; Get the handle of this script with PROCESS_QUERY_INFORMATION (0x0400):</em>
h := DllCall("OpenProcess", "UInt", 0x0400, "Int", false, "UInt", ScriptPID, "Ptr")
<em>; Open an adjustable access token with this process (TOKEN_ADJUST_PRIVILEGES = 32):</em>
DllCall("Advapi32.dll\OpenProcessToken", "Ptr", h, "UInt", 32, "PtrP", t)
<em>; Retrieves the locally unique identifier of the debug privilege:</em>
DllCall("Advapi32.dll\LookupPrivilegeValue", "Ptr", 0, "Str", "SeDebugPrivilege", "Int64P", luid)
ti := BufferAlloc(16, 0)  <em>; structure of privileges</em>
NumPut( "UInt", 1  <em>; one entry in the privileges array...</em>
      , "Int64", luid
      , "UInt", 2  <em>; Enable this privilege: SE_PRIVILEGE_ENABLED = 2</em>
      , ti)
<em>; Update the privileges of this process with the new access token:</em>
r := DllCall("Advapi32.dll\AdjustTokenPrivileges", "Ptr", t, "Int", false, "Ptr", ti, "UInt", 0, "Ptr", 0, "Ptr", 0)
DllCall("CloseHandle", "Ptr", t)  <em>; Close this access token handle to save memory.</em>
DllCall("CloseHandle", "Ptr", h)  <em>; Close this process handle to save memory.</em>

hModule := DllCall("LoadLibrary", "Str", "Psapi.dll")  <em>; Increase performance by preloading the library.</em>
a := BufferAlloc(s)  <em>; An array that receives the list of process identifiers:</em>
c := 0  <em>; counter for process idendifiers</em>
DllCall("Psapi.dll\EnumProcesses", "Ptr", a, "UInt", s, "UIntP", r)
Loop r // 4  <em>; Parse array for identifiers as DWORDs (32 bits):</em>
{
    id := NumGet(a, A_Index * 4, "UInt")
    <em>; Open process with: PROCESS_VM_READ (0x0010) | PROCESS_QUERY_INFORMATION (0x0400)</em>
    h := DllCall("OpenProcess", "UInt", 0x0010 | 0x0400, "Int", false, "UInt", id, "Ptr")
    if !h
        continue
    n := BufferAlloc(s, 0)  <em>; a buffer that receives the base name of the module:</em>
    e := DllCall("Psapi.dll\GetModuleBaseName", "Ptr", h, "Ptr", 0, "Ptr", n, "UInt", s//2)
    if !e    <em>; fall-back method for 64-bit processes when in 32-bit mode:</em>
        e := DllCall("Psapi.dll\GetProcessImageFileName", "Ptr", h, "Ptr", n, "UInt", s//2)
    SplitPath StrGet(n), n
    DllCall("CloseHandle", "Ptr", h)  <em>; close process handle to save memory</em>
    if (n &amp;&amp; e)  <em>; if image is not null add to list:</em>
        l .= n "`n", c++
}
DllCall("FreeLibrary", "Ptr", hModule)  <em>; Unload the library to free memory.</em>
<em>;l := Sort(l)  ; Uncomment this line to sort the list alphabetically.</em>
MsgBox StrReplace(l, "`n", d), c " Processes", 0</pre>
</div>

<div class="ex" id="ExListCOM">
<p><a href="#ExListCOM">#2</a>: Retrieves a list of running processes via COM:</p>
<pre>Gui := GuiCreate(, "Process List")
LV := Gui.Add("ListView", "x2 y0 w400 h500", "Process Name|Command Line")
for process in ComObjGet("winmgmts:").ExecQuery("Select * from Win32_Process")
    LV.Add("", process.Name, process.CommandLine)
Gui.Show

<em>; Win32_Process: <a href="http://msdn.microsoft.com/en-us/library/aa394372.aspx">http://msdn.microsoft.com/en-us/library/aa394372.aspx</a></em>
</pre>
</div>

</body>
</html>